package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcProjectQrsk;
import com.ruoyi.gcgl.domain.GcProjectSqkp;
import com.ruoyi.gcgl.service.IGcProjectQrskService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSqkpService;

/**
 * 确认收款Controller
 * 
 * @author ruoyi
 * @date 2023-01-02
 */
@Controller
@RequestMapping("/gcgl/qrsk")
public class GcProjectQrskController extends BaseController
{
    private String prefix = "gcgl/qrsk";

    @Autowired
    private IGcProjectQrskService gcProjectQrskService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcProjectSqkpService gcProjectSqkpService;

    @RequiresPermissions("gcgl:qrsk:view")
    @GetMapping("/{sqkpId}")
    public String qrsk(ModelMap mmap,@PathVariable("sqkpId") Long sqkpId){
    	mmap.put("sqkpId", sqkpId);
        return prefix + "/qrsk";
    }

    /**
     * 查询确认收款列表
     */
    @PostMapping("/list/{sqkpId}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("sqkpId") Long sqkpId)
    {
        startPage();
        GcProjectQrsk gcProjectQrsk = new GcProjectQrsk();
        gcProjectQrsk.setDeleteFlag(1);
        gcProjectQrsk.setSqkpId(sqkpId);
        List<GcProjectQrsk> list = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
        return getDataTable(list);
    }

    /**
     * 导出确认收款列表
     */
    @RequiresPermissions("gcgl:qrsk:export")
    @Log(title = "确认收款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectQrsk gcProjectQrsk)
    {
        List<GcProjectQrsk> list = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
        ExcelUtil<GcProjectQrsk> util = new ExcelUtil<GcProjectQrsk>(GcProjectQrsk.class);
        return util.exportExcel(list, "确认收款数据");
    }

    /**
     * 新增确认收款
     */
    @GetMapping("/add/{sqkpId}")
    public String add(ModelMap mmap,@PathVariable("sqkpId") Long sqkpId){
    	GcProjectSqkp sqkp = gcProjectSqkpService.selectGcProjectSqkpById(sqkpId);
    	mmap.put("sqkp", sqkp);
    	
    	GcProjectQrsk gcProjectQrsk = new GcProjectQrsk();
    	gcProjectQrsk.setDeleteFlag(1);
    	gcProjectQrsk.setSqkpId(sqkpId);
    	List<GcProjectQrsk> lis = gcProjectQrskService.selectGcProjectQrskList(gcProjectQrsk);
    	
    	double bcsqkpje = Double.parseDouble(sqkp.getItemje().toString());
    	for(GcProjectQrsk qrsk:lis){
    		bcsqkpje -= Double.parseDouble(qrsk.getQrjine().toString());
    	}
    	mmap.put("qrjine", bcsqkpje);
    	
    	GcProject gcProject = gcProjectService.selectGcProjectById(sqkp.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        return prefix + "/add";
    }

    /**
     * 新增保存确认收款
     */
    @RequiresPermissions("gcgl:qrsk:add")
    @Log(title = "确认收款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcProjectQrsk gcProjectQrsk){
    	gcProjectQrsk.setDeleteFlag(1);
    	gcProjectQrsk.setCreateAt(new Date());
    	gcProjectQrsk.setCreateUser(getLoginName());
    	gcProjectQrsk.setUpdateAt(new Date());
    	gcProjectQrsk.setUpdateUser(getLoginName());
    	gcProjectQrskService.insertGcProjectQrsk(gcProjectQrsk);
    	
    	//计算是否更新收款状态
    	GcProjectQrsk obj = new GcProjectQrsk();
    	obj.setDeleteFlag(1);
    	obj.setSqkpId(gcProjectQrsk.getSqkpId());
    	List<GcProjectQrsk> lis = gcProjectQrskService.selectGcProjectQrskList(obj);
    	
    	GcProjectSqkp sqkp = gcProjectSqkpService.selectGcProjectSqkpById(gcProjectQrsk.getSqkpId());
    	double bcsqkpje = Double.parseDouble(sqkp.getItemje().toString());
    	for(GcProjectQrsk qrsk:lis){
    		bcsqkpje -= Double.parseDouble(qrsk.getQrjine().toString());
    	}
    	if(bcsqkpje==0){
    		GcProjectSqkp gcProjectSqkp = new GcProjectSqkp();
    		gcProjectSqkp.setId(gcProjectQrsk.getSqkpId());
    		gcProjectSqkp.setStatus(5);
    		gcProjectSqkp.setUpdateUser(getLoginName());
    		gcProjectSqkp.setUpdateAt(new Date());
    		gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    	}
    	
        return toAjax(1);
    }

    /**
     * 修改确认收款
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectQrsk gcProjectQrsk = gcProjectQrskService.selectGcProjectQrskById(id);
        mmap.put("gcProjectQrsk", gcProjectQrsk);
        
        GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectQrsk.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        
        return prefix + "/edit";
    }

    /**
     * 修改保存确认收款
     */
    @Log(title = "确认收款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectQrsk gcProjectQrsk){
    	gcProjectQrsk.setUpdateUser(getLoginName());
    	gcProjectQrsk.setUpdateAt(new Date());
    	gcProjectQrskService.updateGcProjectQrsk(gcProjectQrsk);
    	
    	//计算是否更新收款状态
    	GcProjectQrsk obj = new GcProjectQrsk();
    	obj.setDeleteFlag(1);
    	obj.setSqkpId(gcProjectQrsk.getSqkpId());
    	List<GcProjectQrsk> lis = gcProjectQrskService.selectGcProjectQrskList(obj);
    	
    	GcProjectSqkp sqkp = gcProjectSqkpService.selectGcProjectSqkpById(gcProjectQrsk.getSqkpId());
    	double bcsqkpje = Double.parseDouble(sqkp.getItemje().toString());
    	for(GcProjectQrsk qrsk:lis){
    		bcsqkpje -= Double.parseDouble(qrsk.getQrjine().toString());
    	}
    	if(bcsqkpje==0){
    		GcProjectSqkp gcProjectSqkp = new GcProjectSqkp();
    		gcProjectSqkp.setId(gcProjectQrsk.getSqkpId());
    		gcProjectSqkp.setStatus(5);
    		gcProjectSqkp.setUpdateUser(getLoginName());
    		gcProjectSqkp.setUpdateAt(new Date());
    		gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    	}
    	
        return toAjax(1);
    }

    /**
     * 删除确认收款
     */
    @RequiresPermissions("gcgl:qrsk:remove")
    @Log(title = "确认收款", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectQrskService.deleteGcProjectQrskByIds(ids));
    }
}
